package com.lineage.server.types;

public class Point {
  protected int _x;
  
  protected int _y;
  
	private static final int HEADING_TABLE_X[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
	private static final int HEADING_TABLE_Y[] = { -1, -1, 0, 1, 1, 1, 0, -1 };

  
  public Point() {
    this._x = 0;
    this._y = 0;
  }
  
  public Point(int x, int y) {
    this._x = 0;
    this._y = 0;
    this._x = x;
    this._y = y;
  }
  
  public Point(Point pt) {
    this._x = 0;
    this._y = 0;
    this._x = pt._x;
    this._y = pt._y;
  }
  
  public int getX() {
    return this._x;
  }
  
  public void setX(int x) {
    this._x = x;
  }
  
  public int getY() {
    return this._y;
  }
  
  public void setY(int y) {
    this._y = y;
  }
  
  public void set(Point pt) {
    this._x = pt._x;
    this._y = pt._y;
  }
  
  public void set(int x, int y) {
    this._x = x;
    this._y = y;
  }
  
  public void forward(int heading) {
    this._x += HEADING_TABLE_X[heading];
    this._y += HEADING_TABLE_Y[heading];
  }
  
  public void backward(int heading) {
    this._x -= HEADING_TABLE_X[heading];
    this._y -= HEADING_TABLE_Y[heading];
  }
  
  public double getLineDistance(Point pt) {
    long diffX = (pt.getX() - getX());
    long diffY = (pt.getY() - getY());
    return Math.sqrt((diffX * diffX + diffY * diffY));
  }
  
  public double getLineDistance(int x, int y) {
    long diffX = (x - getX());
    long diffY = (y - getY());
    return Math.sqrt((diffX * diffX + diffY * diffY));
  }
  
  public int getTileLineDistance(Point pt) {
    return Math.max(Math.abs(pt.getX() - getX()), Math.abs(pt.getY() - getY()));
  }
  
  public int getTileDistance(Point pt) {
    return Math.abs(pt.getX() - getX()) + Math.abs(pt.getY() - getY());
  }
  
  public boolean isInScreen(Point pt) {
    int dist = getTileDistance(pt);
    if (dist > 19)
      return false; 
    if (dist <= 18)
      return true; 
    int dist2 = Math.abs(pt.getX() - getX() - 18) + Math.abs(pt.getY() - getY() - 18);
    return (19 <= dist2 && dist2 <= 52);
  }
  
  public boolean isSamePoint(Point pt) {
    return (pt.getX() == getX() && pt.getY() == getY());
  }
  
  public int hashCode() {
    return 7 * getX() + getY();
  }
  
  public boolean equals(Object obj) {
    if (!(obj instanceof Point))
      return false; 
    Point pt = (Point)obj;
    return (getX() == pt.getX() && getY() == pt.getY());
  }
  
  public String toString() {
    return String.format("(%d, %d)", new Object[] { Integer.valueOf(this._x), Integer.valueOf(this._y) });
  }
}
